package nl.rainbowsheep.reisorg.empl.site.authentication;


import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

/**
 *
 * @author ieme
 */

public class BasicAuthInterceptor implements ClientHttpRequestInterceptor {

    
    @Autowired
    Environment env;

    public BasicAuthInterceptor() {
        
    }
    
    @Override
    public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution ) throws IOException {

        //Build the auth-header
        final String auth = "username1" + ":" + "password1";
        final byte[] encodedAuth = Base64.encodeBase64( auth.getBytes( Charset.forName( "US-ASCII" ) ) );
        final String authHeader = "Basic " + new String( encodedAuth );

        //Add the auth-header
        request.getHeaders().add( "Authorization", authHeader );
        return execution.execute( request, body );
    }

    private String mask( String toMask ) {
        return toMask.replaceAll( ".", "*" );
    }

}
